﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Business.Service;
using Model;

namespace MvcAppWeb.Controllers
{
    public class DCThirdInventoryController : BaseDataController
    {
        public ActionResult DCThirdInventory()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }


        ///<summary>
        ///查询第三方信息
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(string ThirdName, string status)
        {
            string where = "  where 1=1 ";
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            if (ThirdName != "")
            {
                where += " and B.ThirdName like'%" + ThirdName + "%'";
            }
            if (status != "")
            {
                where += " and A.status='" + status + "'";
            }
            var retData = new object();
            BDCThirdInfo db = new BDCThirdInfo();
            retData = db.GetDCThirdInventory(page, rows, out total, where).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
        }



        /// <summary>
        /// 修改or新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string SaveData()
        {
            string sjson = string.Empty;
            try
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                DCThirdInventory _DCThirdInventory = new DCThirdInventory();

                string Id = Request["hiddenID"].Trim();
                string InventoryProportion = Request["txtInventoryProportion"].Trim();
                decimal InventoryRate = Convert.ToDecimal(InventoryProportion);
                if (InventoryRate <= 0 || InventoryRate > 100)
                {
                    sjson = "{success:false,msg:'库存比例设置超出范围！'}";
                    return sjson;
                }

                _DCThirdInventory.THIRDID = Convert.ToInt32(Request["sThirdid"]);
                _DCThirdInventory.INVENTORYPROPORTION = InventoryRate;
                _DCThirdInventory.OPRNO = model.USERCODE.ToString();
                _DCThirdInventory.STATUS = Request["sstatus"].ToString();
                _DCThirdInventory.WARNINGMSG = Convert.ToInt32(Request["txtWarningMsg"]);
                _DCThirdInventory.WAREHOUSEID = Convert.ToInt32(Request["sWareHouseId"]);

                if (!string.IsNullOrEmpty(Id))
                {
                    _DCThirdInventory.ID = int.Parse(Id);
                    _DCThirdInventory.WAREHOUSEID = Convert.ToInt32(Request["hiddenWareHouseId"]);
                }

                BDCThirdInfo db = new BDCThirdInfo();

                if (_DCThirdInventory.WAREHOUSEID == 0)
                {
                    sjson = "{success:false,msg:'请选择仓库！'}";
                }
                else
                {

                    List<DCThirdInventory> lstDCThirdInventory = db.GetDCThirdInventoryByThirdID(_DCThirdInventory.THIRDID)
                        .Where(_ => _.WAREHOUSEID == _DCThirdInventory.WAREHOUSEID && _.ID != _DCThirdInventory.ID).ToList();
                    if (lstDCThirdInventory != null && lstDCThirdInventory.Count > 0)
                    {
                        sjson = "{success:false,msg:'此第三方库存比例已经存在！'}";
                    }
                    else
                    {
                        int n = db.MergeDCThirdInventory(_DCThirdInventory);
                        sjson = "{success:true}";
                    }
                }
                return sjson;
            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;
        }

    }
}
